/**
 * 登录
 * Created by douxc on 2015/4/20.
 */
define(function (require, exports, module) {
  'use strict';
  module.exports = function (app) {
    app.register.controller("LoginCtrl", ["$scope", "MHService", function ($scope, MHService) {
    }]);
    app.register.directive('loginBtn', function () {
      return {
        restrict: 'E',
        replace: true,
        controller: function ($scope, MHService, $location,$http) {
          $scope.login = function () {
            var tUrl = MHService.URL.login;
            var parameter ={
                username : $scope.userInfo.username,
                userpwd : $scope.userInfo.password
            };
            $http.post(tUrl,parameter)
            .success(function(data){
              // console.log(data);
                if (data['success']==true&&MHService.globalClearAll()) {
                    MHService.globalData(true, {key: 'isLogin', value: 'true'});
                    MHService.Toast(data['msg']);
                    $location.path('/personal');
                } else {
                    MHService.Toast(data['msg']);
                }
            })
            .error(function(data){
                //查询失败，展示错误信息
                    MHService.Toast('操作失败，请刷新重试 -s');
                if (data.match('<h1>404 Not Found</h1>')) {
                    $timeout(function () {
                        $location.path('/login');
                    }, 500);
                }
            });
          }
          // $scope.login = function () {
          //   var tUrl = MHService.URL.login + '?username=' + $scope.userInfo.username + '&userpwd=' + $scope.userInfo.password;
          //  console.log(tUrl);
          //   MHService.getReq({url: tUrl}, function (data) {
          //     if (MHService.globalClearAll()) {
          //       MHService.globalData(true, {key: 'isLogin', value: 'true'});
          //       //alert('ok');
          //     } else {
          //       //alert('false');
          //       MHService.globalData(true, {key: 'isLogin', value: 'true'});
          //     }
          //     $location.path('/personal');
          //   });
          // }
        },
        template: '<button type="submit" class="am-btn am-btn-default am-form-field am-radius mh-deepgreen mh-text-white">登 录' +
        '</button>'
      }
    });
  };
});
